home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / system-config-printer / jobviewer.py < prev    next >
Encoding:
Python Source  |  2009-05-05  |  60.2 KB  |  1,572 lines

  1.  
  2. ## Copyright (C) 2007, 2008, 2009 Tim Waugh <twaugh@redhat.com>
  3. ## Copyright (C) 2007, 2008, 2009 Red Hat, Inc.
  4.  
  5. ## This program is free software; you can redistribute it and/or modify
  6. ## it under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2 of the License, or
  8. ## (at your option) any later version.
  9.  
  10. ## This program is distributed in the hope that it will be useful,
  11. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ## GNU General Public License for more details.
  14.  
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with this program; if not, write to the Free Software
  17. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. import authconn
  20. import cups
  21. import dbus
  22. import dbus.glib
  23. import dbus.service
  24. import pynotify
  25. import gettext
  26. import gobject
  27. import gtk
  28. import gtk.gdk
  29. import gtk.glade
  30. from glade import GtkGUI
  31. import monitor
  32. import os
  33. import pango
  34. import pwd
  35. import smburi
  36. import subprocess
  37. import sys
  38. import time
  39. import urllib
  40.  
  41. from debug import *
  42. import config
  43. import statereason
  44. import errordialogs
  45. import pprint
  46.  
  47. try:
  48.     import gnomekeyring
  49.     USE_KEYRING=True
  50. except ImportError:
  51.     USE_KEYRING=False
  52.  
  53. from gettext import gettext as _
  54. DOMAIN="system-config-printer"
  55. gettext.textdomain (DOMAIN)
  56. gtk.glade.textdomain (DOMAIN)
  57. from statereason import StateReason
  58. statereason.set_gettext_function (_)
  59. errordialogs.set_gettext_function (_)
  60.  
  61. pkgdata = config.pkgdatadir
  62. GLADE="applet.glade"
  63. ICON="printer"
  64. SEARCHING_ICON="document-print-preview"
  65.  
  66. # We need to call pynotify.init before we can check the server for caps
  67. pynotify.init('System Config Printer Notification')
  68.  
  69. class PrinterURIIndex:
  70.     def __init__ (self, names=None):
  71.         self.printer = {}
  72.         self.names = names
  73.  
  74.     def update_from_attrs (self, printer, attrs):
  75.         uris = []
  76.         if attrs.has_key ('printer-uri-supported'):
  77.             uri_supported = attrs['printer-uri-supported']
  78.             if type (uri_supported) != list:
  79.                 uri_supported = [uri_supported]
  80.             uris.extend (uri_supported)
  81.         if attrs.has_key ('notify-printer-uri'):
  82.             uris.append (attrs['notify-printer-uri'])
  83.         if attrs.has_key ('printer-more-info'):
  84.             uris.append (attrs['printer-more-info'])
  85.  
  86.         for uri in uris:
  87.             self.printer[uri] = printer
  88.  
  89.     def remove_printer (self, printer):
  90.         # Remove references to this printer in the URI map.
  91.         uris = self.printer.keys ()
  92.         for uri in uris:
  93.             if self.printer[uri] == printer:
  94.                 del self.printer[uri]
  95.  
  96.     def lookup (self, uri, connection=None):
  97.         try:
  98.             return self.printer[uri]
  99.         except KeyError:
  100.             if connection == None:
  101.                 connection = cups.Connection ()
  102.  
  103.             r = ['printer-name', 'printer-uri-supported', 'printer-more-info']
  104.             try:
  105.                 attrs = connection.getPrinterAttributes (uri=uri,
  106.                                                          requested_attributes=r)
  107.             except cups.IPPError:
  108.                 # URI not known.
  109.                 raise KeyError
  110.  
  111.             name = attrs['printer-name']
  112.             self.update_from_attrs (name, attrs)
  113.             self.printer[uri] = name
  114.             try:
  115.                 return self.printer[uri]
  116.             except KeyError:
  117.                 pass
  118.         raise KeyError
  119.  
  120.  
  121. class JobViewer (GtkGUI, monitor.Watcher):
  122.     required_job_attributes = set(['job-k-octets',
  123.                                    'job-name',
  124.                                    'job-originating-user-name',
  125.                                    'job-printer-uri',
  126.                                    'job-state',
  127.                                    'time-at-creation'])
  128.  
  129.     def __init__(self, bus=None, loop=None, service_running=False,
  130.                  trayicon=False, suppress_icon_hide=False,
  131.                  my_jobs=True, specific_dests=None, exit_handler=None,
  132.                  parent=None):
  133.         self.loop = loop
  134.         self.service_running = service_running
  135.         self.trayicon = trayicon
  136.         self.suppress_icon_hide = suppress_icon_hide
  137.         self.my_jobs = my_jobs
  138.         self.specific_dests = specific_dests
  139.         self.exit_handler = exit_handler
  140.  
  141.         self.jobs = {}
  142.         self.jobiters = {}
  143.         self.active_jobs = set() # of job IDs
  144.         self.stopped_job_prompts = set() # of job IDs
  145.         self.printer_state_reasons = {}
  146.         self.num_jobs_when_hidden = 0
  147.         self.connecting_to_device = {} # dict of printer->time first seen
  148.         self.state_reason_notifications = {}
  149.         self.auth_info_dialogs = {} # by job ID
  150.         self.job_creation_times_timer = None
  151.         self.special_status_icon = False
  152.         self.new_printer_notifications = {}
  153.         self.completed_job_notifications = {}
  154.         self.authenticated_jobs = set() # of job IDs
  155.  
  156.         self.getWidgets ({"JobsWindow":
  157.                               ["JobsWindow",
  158.                                "job_menubar_item",
  159.                                "treeview",
  160.                                "statusbar"],
  161.                           "statusicon_popupmenu":
  162.                               ["statusicon_popupmenu"]})
  163.  
  164.         job_action_group = gtk.ActionGroup ("JobActionGroup")
  165.         job_action_group.add_actions ([
  166.                 ("cancel-job", gtk.STOCK_CANCEL, None, None, None,
  167.                  self.on_job_cancel_activate),
  168.                 ("hold-job", gtk.STOCK_MEDIA_PAUSE, _("_Hold"), None, None,
  169.                  self.on_job_hold_activate),
  170.                 ("release-job", gtk.STOCK_MEDIA_PLAY, _("_Release"), None, None,
  171.                  self.on_job_release_activate),
  172.                 ("reprint-job", gtk.STOCK_REDO, _("Re_print"), None, None,
  173.                  self.on_job_reprint_activate),
  174.                 ("authenticate-job", None, _("_Authenticate"), None, None,
  175.                  self.on_job_authenticate_activate)
  176.                 ])
  177.         self.job_ui_manager = gtk.UIManager ()
  178.         self.job_ui_manager.insert_action_group (job_action_group, -1)
  179.         self.job_ui_manager.add_ui_from_string (
  180. """
  181. <ui>
  182.  <accelerator action="cancel-job"/>
  183.  <accelerator action="hold-job"/>
  184.  <accelerator action="release-job"/>
  185.  <accelerator action="reprint-job"/>
  186.  <accelerator action="authenticate-job"/>
  187. </ui>
  188. """
  189. )
  190.         self.job_ui_manager.ensure_update ()
  191.         self.JobsWindow.add_accel_group (self.job_ui_manager.get_accel_group ())
  192.         self.job_context_menu = gtk.Menu ()
  193.         for action_name in ["cancel-job",
  194.                             "hold-job",
  195.                             "release-job",
  196.                             "reprint-job",
  197.                             None,
  198.                             "authenticate-job"]:
  199.             if not action_name:
  200.                 item = gtk.SeparatorMenuItem ()
  201.             else:
  202.                 action = job_action_group.get_action (action_name)
  203.                 action.set_sensitive (False)
  204.                 item = action.create_menu_item ()
  205.  
  206.             item.show ()
  207.             self.job_context_menu.append (item)
  208.  
  209.         self.job_menubar_item.set_submenu (self.job_context_menu)
  210.  
  211.         text=0
  212.         for name in [_("Job"),
  213.                      _("User"),
  214.                      _("Document"),
  215.                      _("Printer"),
  216.                      _("Size"),
  217.                      _("Time submitted"),
  218.                      _("Status")]:
  219.             if text == 1 and trayicon:
  220.                 # Skip the user column for the trayicon.
  221.                 text += 1
  222.                 continue
  223.             cell = gtk.CellRendererText()
  224.             if text == 2 or text == 3:
  225.                 # Ellipsize the 'Document' and 'Printer' columns.
  226.                 cell.set_property ("ellipsize", pango.ELLIPSIZE_END)
  227.                 cell.set_property ("width-chars", 20)
  228.             column = gtk.TreeViewColumn(name, cell, text=text)
  229.             column.set_resizable(True)
  230.             self.treeview.append_column(column)
  231.             text += 1
  232.  
  233.         self.treeview.get_selection().set_mode(gtk.SELECTION_SINGLE)
  234.         self.store = gtk.TreeStore(int, str, str, str, str, str, str)
  235.         self.store.set_sort_column_id (0, gtk.SORT_DESCENDING)
  236.         self.treeview.set_model(self.store)
  237.         self.treeview.set_rules_hint (True)
  238.         self.treeview.connect ('button_release_event',
  239.                                self.on_treeview_button_release_event)
  240.         self.treeview.connect ('popup-menu', self.on_treeview_popup_menu)
  241.         self.treeview.connect ('cursor-changed',
  242.                                self.on_treeview_cursor_changed)
  243.  
  244.         self.JobsWindow.set_icon_name (ICON)
  245.         self.JobsWindow.hide ()
  246.  
  247.         if specific_dests:
  248.             the_dests = reduce (lambda x, y: x + ", " + y, specific_dests)
  249.  
  250.         if my_jobs:
  251.             if specific_dests:
  252.                 title = _("my jobs on %s") % the_dests
  253.             else:
  254.                 title = _("my jobs")
  255.         else:
  256.             if specific_dests:
  257.                 title = "%s" % the_dests
  258.             else:
  259.                 title = _("all jobs")
  260.         self.JobsWindow.set_title (_("Document Print Status (%s)") % title)
  261.  
  262.         if parent:
  263.             self.JobsWindow.set_transient_for (parent)
  264.  
  265.         self.statusbar_set = False
  266.  
  267.         if self.trayicon:
  268.             self.statusicon = gtk.StatusIcon ()
  269.             theme = gtk.icon_theme_get_default ()
  270.             pixbuf = theme.load_icon (ICON, 22, 0)
  271.             self.statusicon.set_from_pixbuf (pixbuf)
  272.             self.icon_jobs = self.statusicon.get_pixbuf ()
  273.             self.icon_jobs_processing = theme.load_icon ("printer-printing",
  274.                                                          22, 0)
  275.             self.icon_no_jobs = self.icon_jobs.copy ()
  276.             self.icon_no_jobs.fill (0)
  277.             self.icon_jobs.composite (self.icon_no_jobs,
  278.                                       0, 0,
  279.                                       self.icon_no_jobs.get_width(),
  280.                                       self.icon_no_jobs.get_height(),
  281.                                       0, 0,
  282.                                       1.0, 1.0,
  283.                                       gtk.gdk.INTERP_BILINEAR,
  284.                                       127)
  285.             self.set_statusicon_from_pixbuf (self.icon_no_jobs)
  286.             self.statusicon.connect ('activate', self.toggle_window_display)
  287.             self.statusicon.connect ('popup-menu', self.on_icon_popupmenu)
  288.             self.statusicon.set_visible (False)
  289.  
  290.         # D-Bus
  291.         if bus == None:
  292.             bus = dbus.SystemBus ()
  293.  
  294.         self.set_process_pending (True)
  295.         self.host = cups.getServer ()
  296.         self.port = cups.getPort ()
  297.         self.encryption = cups.getEncryption ()
  298.         self.monitor = monitor.Monitor (self, bus=bus, my_jobs=my_jobs,
  299.                                         specific_dests=specific_dests,
  300.                                         host=self.host, port=self.port,
  301.                                         encryption=self.encryption)
  302.  
  303.         if not self.trayicon:
  304.             self.JobsWindow.show ()
  305.  
  306.     def cleanup (self):
  307.         self.monitor.cleanup ()
  308.  
  309.         # Close any open notifications.
  310.         for l in [self.new_printer_notifications.values (),
  311.                   self.state_reason_notifications.values ()]:
  312.             for notification in l:
  313.                 if notification.get_data ('closed') != True:
  314.                     notification.close ()
  315.                     notification.set_data ('closed', True)
  316.  
  317.         if self.exit_handler:
  318.             self.exit_handler (self)
  319.  
  320.     def set_process_pending (self, whether):
  321.         self.process_pending_events = whether
  322.  
  323.     # Handle "special" status icon
  324.     def set_special_statusicon (self, iconname):
  325.         self.special_status_icon = True
  326.         self.statusicon.set_from_icon_name (iconname)
  327.         self.set_statusicon_visibility ()
  328.  
  329.     def unset_special_statusicon (self):
  330.         self.special_status_icon = False
  331.         self.statusicon.set_from_pixbuf (self.saved_statusicon_pixbuf)
  332.         self.set_statusicon_visibility ()
  333.  
  334.     def notify_new_printer (self, printer, notification):
  335.         self.new_printer_notifications[printer] = notification
  336.         notification.set_data ('printer-name', printer)
  337.         notification.connect ('closed', self.on_new_printer_notification_closed)
  338.         self.set_statusicon_visibility ()
  339.         notification.attach_to_status_icon (self.statusicon)
  340.         notification.show ()
  341.  
  342.     def on_new_printer_notification_closed (self, notification, reason=None):
  343.         printer = notification.get_data ('printer-name')
  344.         del self.new_printer_notifications[printer]
  345.         self.set_statusicon_visibility ()
  346.  
  347.     def set_statusicon_from_pixbuf (self, pb):
  348.         self.saved_statusicon_pixbuf = pb
  349.         if not self.special_status_icon:
  350.             self.statusicon.set_from_pixbuf (pb)
  351.  
  352.     def on_delete_event(self, *args):
  353.         if self.trayicon or not self.loop:
  354.             self.JobsWindow.hide ()
  355.             if not self.loop:
  356.                 # Being run from main app, not applet
  357.                 self.cleanup ()
  358.         else:
  359.             self.loop.quit ()
  360.         return True
  361.  
  362.     def show_IPP_Error(self, exception, message):
  363.         return errordialogs.show_IPP_Error (exception, message, self.JobsWindow)
  364.  
  365.     def toggle_window_display(self, icon, force_show=False):
  366.         visible = self.JobsWindow.get_property('visible')
  367.         if force_show:
  368.             visible = False
  369.  
  370.         if visible:
  371.             self.JobsWindow.hide()
  372.         else:
  373.             self.JobsWindow.show()
  374.  
  375.     def on_show_completed_jobs_activate(self, menuitem):
  376.         if menuitem.get_active():
  377.             which_jobs = "all"
  378.         else:
  379.             which_jobs = "not-completed"
  380.         self.monitor.refresh(which_jobs=which_jobs, refresh_all=False)
  381.  
  382.     def update_job_creation_times(self):
  383.         now = time.time ()
  384.         need_update = False
  385.         for job, data in self.jobs.iteritems():
  386.             if self.jobs.has_key (job):
  387.                 iter = self.jobiters[job]
  388.  
  389.             t = _("Unknown")
  390.             if data.has_key ('time-at-creation'):
  391.                 created = data['time-at-creation']
  392.                 ago = now - created
  393.                 need_update = True
  394.                 if ago < 2 * 60:
  395.                     t = _("a minute ago")
  396.                 elif ago < 60 * 60:
  397.                     mins = int (ago / 60)
  398.                     t = _("%d minutes ago") % mins
  399.                 elif ago < 24 * 60 * 60:
  400.                     hours = int (ago / (60 * 60))
  401.                     if hours == 1:
  402.                         t = _("an hour ago")
  403.                     else:
  404.                         t = _("%d hours ago") % hours
  405.                 elif ago < 7 * 24 * 60 * 60:
  406.                     days = int (ago / (24 * 60 * 60))
  407.                     if days == 1:
  408.                         t = _("yesterday")
  409.                     else:
  410.                         t = _("%d days ago") % days
  411.                 elif ago < 6 * 7 * 24 * 60 * 60:
  412.                     weeks = int (ago / (7 * 24 * 60 * 60))
  413.                     if weeks == 1:
  414.                         t = _("last week")
  415.                     else:
  416.                         t = _("%d weeks ago") % weeks
  417.                 else:
  418.                     need_update = False
  419.                     t = time.strftime ("%B %Y", time.localtime (created))
  420.  
  421.             self.store.set_value (iter, 5, t)
  422.  
  423.         if need_update and not self.job_creation_times_timer:
  424.             t = gobject.timeout_add (60 * 1000, self.update_job_creation_times)
  425.             self.job_creation_times_timer = t
  426.  
  427.         if not need_update:
  428.             if self.job_creation_times_timer:
  429.                 gobject.source_remove (self.job_creation_times_timer)
  430.                 self.job_creation_times_timer = None
  431.  
  432.         # Return code controls whether the timeout will recur.
  433.         return need_update
  434.  
  435.     def print_error_dialog_response(self, dialog, response, jobid):
  436.         dialog.hide ()
  437.         dialog.destroy ()
  438.         self.stopped_job_prompts.remove (jobid)
  439.         if response == gtk.RESPONSE_NO:
  440.             # Diagnose
  441.             if not self.__dict__.has_key ('troubleshooter'):
  442.                 import troubleshoot
  443.                 troubleshooter = troubleshoot.run (self.on_troubleshoot_quit)
  444.                 self.troubleshooter = troubleshooter
  445.  
  446.     def on_troubleshoot_quit(self, troubleshooter):
  447.         del self.troubleshooter
  448.  
  449.     def add_job (self, job, data, connection=None):
  450.         store = self.store
  451.         iter = self.store.append (None)
  452.         store.set_value (iter, 0, job)
  453.         store.set_value (iter, 1, data.get('job-originating-user-name',
  454.                                            _("Unknown")))
  455.         store.set_value (iter, 2, data.get('job-name', _("Unknown")))
  456.         debugprint ("Job %d added" % job)
  457.         self.jobiters[job] = iter
  458.         if not self.job_creation_times_timer:
  459.             t = gobject.timeout_add (1000, self.update_job_creation_times)
  460.             self.job_creation_times_timer = t
  461.  
  462.         self.update_job (job, data, connection=connection)
  463.  
  464.     def update_job (self, job, data, connection=None):
  465.         # Fetch required attributes for this job if they are missing.
  466.         r = self.required_job_attributes - set (data.keys ())
  467.  
  468.         if r:
  469.             attrs = None
  470.             try:
  471.                 if connection == None:
  472.                     connection = cups.Connection (host=self.host,
  473.                                                   port=self.port,
  474.                                                   encryption=self.encryption)
  475.  
  476.                 debugprint ("requesting %s" % r)
  477.                 r = list (r)
  478.                 attrs = connection.getJobAttributes (job,
  479.                                                      requested_attributes=r)
  480.             except RuntimeError:
  481.                 pass
  482.             except AttributeError:
  483.                 pass
  484.  
  485.             if attrs:
  486.                 data.update (attrs)
  487.  
  488.         store = self.store
  489.         iter = self.jobiters[job]
  490.         self.jobs[job] = data
  491.  
  492.         printer = data['job-printer-name']
  493.         store.set_value (iter, 3, printer)
  494.  
  495.         size = _("Unknown")
  496.         if data.has_key ('job-k-octets'):
  497.             size = str (data['job-k-octets']) + 'k'
  498.         store.set_value (iter, 4, size)
  499.  
  500.         job_requires_auth = False
  501.         c = None
  502.         try:
  503.             jstate = data.get ('job-state', cups.IPP_JOB_PROCESSING)
  504.             s = int (jstate)
  505.  
  506.             if s in [cups.IPP_JOB_HELD, cups.IPP_JOB_STOPPED]:
  507.                 jattrs = ['job-state', 'job-hold-until']
  508.                 pattrs = ['auth-info-required', 'device-uri']
  509.                 uri = data.get ('job-printer-uri')
  510.                 c = authconn.Connection (self.JobsWindow,
  511.                                          host=self.host,
  512.                                          port=self.port,
  513.                                          encryption=self.encryption)
  514.                 attrs = c.getPrinterAttributes (uri = uri,
  515.                                                 requested_attributes=pattrs)
  516.  
  517.                 try:
  518.                     auth_info_required = attrs['auth-info-required']
  519.                 except KeyError:
  520.                     debugprint ("No auth-info-required attribute; "
  521.                                 "guessing instead")
  522.                     auth_info_required = ['username', 'password']
  523.  
  524.                 if not isinstance (auth_info_required, list):
  525.                     auth_info_required = [auth_info_required]
  526.                     attrs['auth-info-required'] = auth_info_required
  527.  
  528.                 data.update (attrs)
  529.  
  530.                 attrs = c.getJobAttributes (job,
  531.                                             requested_attributes=jattrs)
  532.                 data.update (attrs)
  533.                 jstate = data.get ('job-state', cups.IPP_JOB_PROCESSING)
  534.                 s = int (jstate)
  535.         except ValueError:
  536.             pass
  537.         except RuntimeError:
  538.             pass
  539.         except cups.IPPError, (e, m):
  540.             pass
  541.  
  542.         job_requires_auth = (s == cups.IPP_JOB_HELD and
  543.                              data.get ('job-hold-until', 'none') ==
  544.                              'auth-info-required')
  545.         state = None
  546.         if job_requires_auth:
  547.             state = _("Held for authentication")
  548.         elif s == cups.IPP_JOB_HELD:
  549.             state = _("Held")
  550.             until = data.get ('job-hold-until')
  551.             if until != None:
  552.                 try:
  553.                     colon1 = until.find (':')
  554.                     if colon1 != -1:
  555.                         now = time.gmtime ()
  556.                         hh = int (until[:colon1])
  557.                         colon2 = until[colon1 + 1:].find (':')
  558.                         if colon2 != -1:
  559.                             colon2 += colon1 + 1
  560.                             mm = int (until[colon1 + 1:colon2])
  561.                             ss = int (until[colon2 + 1:])
  562.                         else:
  563.                             mm = int (until[colon1 + 1:])
  564.                             ss = 0
  565.  
  566.                         day = now.tm_mday
  567.                         if (hh < now.tm_hour or
  568.                             hh == now.tm_hour and
  569.                             (mm < now.tm_min or
  570.                              mm == now.tm_min and ss < now.tm_sec)):
  571.                             day += 1
  572.  
  573.                         hold = (now.tm_year, now.tm_mon, day,
  574.                                 hh, mm, ss, 0, 0, -1)
  575.                         local = time.localtime (time.mktime (hold) - time.timezone)
  576.                         state = _("Held until %s") % time.strftime ("%X", local)
  577.                 except ValueError:
  578.                     pass
  579.             if until == "day-time":
  580.                 state = _("Held until day-time")
  581.             elif until == "evening":
  582.                 state = _("Held until evening")
  583.             elif until == "night":
  584.                 state = _("Held until night-time")
  585.             elif until == "second-shift":
  586.                 state = _("Held until second shift")
  587.             elif until == "third-shift":
  588.                 state = _("Held until third shift")
  589.             elif until == "weekend":
  590.                 state = _("Held until weekend")
  591.         else:
  592.             try:
  593.                 state = { cups.IPP_JOB_PENDING: _("Pending"),
  594.                           cups.IPP_JOB_PROCESSING: _("Processing"),
  595.                           cups.IPP_JOB_STOPPED: _("Stopped"),
  596.                           cups.IPP_JOB_CANCELED: _("Canceled"),
  597.                           cups.IPP_JOB_ABORTED: _("Aborted"),
  598.                           cups.IPP_JOB_COMPLETED: _("Completed") }[s]
  599.             except IndexError:
  600.                 pass
  601.  
  602.         if state == None:
  603.             state = _("Unknown")
  604.         store.set_value (iter, 6, state)
  605.  
  606.         # Check whether authentication is required.
  607.         if self.trayicon:
  608.             if (job_requires_auth and
  609.                 not self.auth_info_dialogs.has_key (job)):
  610.                 try:
  611.                     cups.require ("1.9.37")
  612.                 except:
  613.                     debugprint ("Authentication required but "
  614.                                 "authenticateJob() not available")
  615.                     return
  616.  
  617.                 # Find out which auth-info is required.
  618.                 try_keyring = USE_KEYRING
  619.                 keyring_attrs = None
  620.                 auth_info = None
  621.                 if try_keyring and 'password' in auth_info_required:
  622.                     auth_info_required = data.get ('auth-info-required', [])
  623.                     device_uri = data.get ("device-uri")
  624.                     (scheme, rest) = urllib.splittype (device_uri)
  625.                     keyring_attrs = dict ()
  626.                     if scheme == 'smb':
  627.                         uri = smburi.SMBURI (uri=device_uri)
  628.                         (group, server, share,
  629.                          user, password) = uri.separate ()
  630.                         keyring_attrs["domain"] = str (group)
  631.                     else:
  632.                         (serverport, rest) = urllib.splithost (rest)
  633.                         (server, port) = urllib.splitnport (serverport)
  634.                     username = pwd.getpwuid (os.getuid ())[0]
  635.                     keyring_attrs.update ({ "server": str (server.lower ()),
  636.                                             "protocol": str (scheme),
  637.                                             "user": str (username)})
  638.  
  639.                 if job in self.authenticated_jobs:
  640.                     # We've already tried to authenticate this job before.
  641.                     try_keyring = False
  642.  
  643.                 if try_keyring and 'password' in auth_info_required:
  644.                     type = gnomekeyring.ITEM_NETWORK_PASSWORD
  645.                     try:
  646.                         items = gnomekeyring.find_items_sync (type,
  647.                                                               keyring_attrs)
  648.                         auth_info = map (lambda x: '', auth_info_required)
  649.                         ind = auth_info_required.index ('username')
  650.                         auth_info[ind] = items[0].attributes.get ('user', '')
  651.                         ind = auth_info_required.index ('password')
  652.                         auth_info[ind] = items[0].secret
  653.                     except gnomekeyring.NoMatchError:
  654.                         debugprint ("gnomekeyring: no match for %s" %
  655.                                     keyring_attrs)
  656.                     except gnomekeyring.DeniedError:
  657.                         debugprint ("gnomekeyring: denied for %s" %
  658.                                     keyring_attrs)
  659.  
  660.                 if try_keyring and c == None:
  661.                     try:
  662.                         c = authconn.Connection (self.JobsWindow,
  663.                                                  host=self.host,
  664.                                                  port=self.port,
  665.                                                  encryption=self.encryption)
  666.                     except RuntimeError:
  667.                         try_keyring = False
  668.  
  669.                 if try_keyring and auth_info != None:
  670.                     try:
  671.                         c._begin_operation (_("authenticating job"))
  672.                         c.authenticateJob (job, auth_info)
  673.                         c._end_operation ()
  674.                         self.monitor.update ()
  675.                         debugprint ("Automatically authenticated job %d" % job)
  676.                         self.authenticated_jobs.add (job)
  677.                         return
  678.                     except cups.IPPError, (e, m):
  679.                         c._end_operation ()
  680.                         nonfatalException ()
  681.                         return
  682.                     except:
  683.                         c._end_operation ()
  684.                         nonfatalException ()
  685.  
  686.                 self.display_auth_info_dialog (job)
  687.  
  688.     def on_auth_notification_closed (self, notification, reason=None):
  689.         job = notification.get_data ('job-id')
  690.         debugprint ("auth notification closed for job %s" % job)
  691.         self.auth_notifications[job].set_data ('closed', True)
  692.         del self.auth_notifications[job]
  693.  
  694.     def on_auth_notification_authenticate (self, notification, action):
  695.         job = notification.get_data ('job-id')
  696.         keyring_attrs = notification.get_data ('keyring-attrs')
  697.         debugprint ("auth notification authenticate for job %s" % job)
  698.         self.display_auth_info_dialog (job, keyring_attrs)
  699.  
  700.     def display_auth_info_dialog (self, job, keyring_attrs=None):
  701.         data = self.jobs[job]
  702.         auth_info_required = data['auth-info-required']
  703.         dialog = authconn.AuthDialog (auth_info_required=auth_info_required,
  704.                                       allow_remember=USE_KEYRING)
  705.         dialog.set_data ('keyring-attrs', keyring_attrs)
  706.         dialog.set_data ('auth-info-required', auth_info_required)
  707.         dialog.set_position (gtk.WIN_POS_CENTER)
  708.  
  709.         # Pre-fill 'username' field.
  710.         auth_info = map (lambda x: '', auth_info_required)
  711.         username = pwd.getpwuid (os.getuid ())[0]
  712.         if 'username' in auth_info_required:
  713.             try:
  714.                 ind = auth_info_required.index ('username')
  715.                 auth_info[ind] = username
  716.                 dialog.set_auth_info (auth_info)
  717.             except:
  718.                 nonfatalException ()
  719.  
  720.         # Focus on the first empty field.
  721.         index = 0
  722.         for field in auth_info_required:
  723.             if auth_info[index] == '':
  724.                 dialog.field_grab_focus (field)
  725.                 break
  726.             index += 1
  727.  
  728.         dialog.set_prompt (_("Authentication required for "
  729.                              "printing document `%s' (job %d)") %
  730.                            (data.get('job-name', _("Unknown")), job))
  731.         self.auth_info_dialogs[job] = dialog
  732.         dialog.connect ('response', self.auth_info_dialog_response)
  733.         dialog.connect ('delete-event', self.auth_info_dialog_delete)
  734.         dialog.set_data ('job-id', job)
  735.         dialog.show_all ()
  736.         dialog.set_keep_above (True)
  737.         dialog.show_now ()
  738.  
  739.     def auth_info_dialog_delete (self, dialog, event):
  740.         self.auth_info_dialog_response (dialog, gtk.RESPONSE_CANCEL)
  741.  
  742.     def auth_info_dialog_response (self, dialog, response):
  743.         jobid = dialog.get_data ('job-id')
  744.         del self.auth_info_dialogs[jobid]
  745.         if response != gtk.RESPONSE_OK:
  746.             dialog.destroy ()
  747.             return
  748.  
  749.         auth_info = dialog.get_auth_info ()
  750.         try:
  751.             c = authconn.Connection (self.JobsWindow,
  752.                                      host=self.host,
  753.                                      port=self.port,
  754.                                      encryption=self.encryption)
  755.         except RuntimeError:
  756.             debugprint ("Error connecting to CUPS for authentication")
  757.             return
  758.  
  759.         remember = False
  760.         c._begin_operation (_("authenticating job"))
  761.         try:
  762.             c.authenticateJob (jobid, auth_info)
  763.             remember = dialog.get_remember_password ()
  764.             self.authenticated_jobs.add (jobid)
  765.             self.monitor.update ()
  766.         except cups.IPPError, (e, m):
  767.             self.show_IPP_Error (e, m)
  768.  
  769.         c._end_operation ()
  770.  
  771.         if remember:
  772.             try:
  773.                 keyring = gnomekeyring.get_default_keyring_sync ()
  774.                 type = gnomekeyring.ITEM_NETWORK_PASSWORD
  775.                 attrs = dialog.get_data ("keyring-attrs")
  776.                 auth_info_required = dialog.get_data ('auth-info-required')
  777.                 try:
  778.                     ind = auth_info_required.index ('username')
  779.                     attrs['user'] = auth_info[ind]
  780.                 except IndexError:
  781.                     pass
  782.  
  783.                 if attrs != None:
  784.                     name = "%s@%s (%s)" % (attrs.get ("user"),
  785.                                            attrs.get ("server"),
  786.                                            attrs.get ("protocol"))
  787.                     ind = auth_info_required.index ('password')
  788.                     secret = auth_info[ind]
  789.                     gnomekeyring.item_create_sync (keyring, type, name,
  790.                                                    attrs, secret, True)
  791.             except:
  792.                 nonfatalException ()
  793.  
  794.         dialog.destroy ()
  795.  
  796.     def set_statusicon_visibility (self):
  797.         if not self.trayicon:
  798.             return
  799.  
  800.         if self.suppress_icon_hide:
  801.             # Avoid hiding the icon if we've been woken up to notify
  802.             # about a new printer.
  803.             self.suppress_icon_hide = False
  804.             return
  805.  
  806.         open_notifications = len (self.new_printer_notifications.keys ())
  807.         open_notifications += len (self.completed_job_notifications.keys ())
  808.         for reason, notification in self.state_reason_notifications.iteritems():
  809.             if notification.get_data ('closed') != True:
  810.                 open_notifications += 1
  811.         num_jobs = len (self.active_jobs)
  812.  
  813.         debugprint ("open notifications: %d" % open_notifications)
  814.         debugprint ("num_jobs: %d" % num_jobs)
  815.         debugprint ("num_jobs_when_hidden: %d" % self.num_jobs_when_hidden)
  816.  
  817.         self.statusicon.set_visible (self.special_status_icon or
  818.                                      open_notifications > 0 or
  819.                                      num_jobs > self.num_jobs_when_hidden)
  820.  
  821.         # Let the icon show/hide itself before continuing.
  822.         while self.process_pending_events and gtk.events_pending ():
  823.             gtk.main_iteration ()
  824.  
  825.     def on_treeview_popup_menu (self, treeview):
  826.         event = gtk.gdk.Event (gtk.gdk.NOTHING)
  827.         self.show_treeview_popup_menu (treeview, event, 0)
  828.  
  829.     def on_treeview_button_release_event(self, treeview, event):
  830.         if event.button == 3:
  831.             self.show_treeview_popup_menu (treeview, event, event.button)
  832.  
  833.     def on_treeview_cursor_changed (self, treeview):
  834.         path, column = treeview.get_cursor ()
  835.         cancel = self.job_ui_manager.get_action ("/cancel-job")
  836.         hold = self.job_ui_manager.get_action ("/hold-job")
  837.         release = self.job_ui_manager.get_action ("/release-job")
  838.         reprint = self.job_ui_manager.get_action ("/reprint-job")
  839.         authenticate = self.job_ui_manager.get_action ("/authenticate-job")
  840.         if path == None:
  841.             for widget in [cancel, hold, release, reprint, authenticate]:
  842.                 widget.set_sensitive (False)
  843.             return
  844.  
  845.         iter = self.store.get_iter (path)
  846.         self.jobid = self.store.get_value (iter, 0)
  847.         job = self.jobs[self.jobid]
  848.         authenticate.set_sensitive (False)
  849.         for widget in [cancel, hold, release, reprint]:
  850.             widget.set_sensitive (True)
  851.  
  852.         if job.has_key ('job-state'):
  853.             s = job['job-state']
  854.             if s >= cups.IPP_JOB_CANCELED:
  855.                 cancel.set_sensitive (False)
  856.             if s != cups.IPP_JOB_PENDING:
  857.                 hold.set_sensitive (False)
  858.             if s != cups.IPP_JOB_HELD:
  859.                 release.set_sensitive (False)
  860.             if (not job.get('job-preserved', False)):
  861.                 reprint.set_sensitive (False)
  862.  
  863.         if job.get ('job-state', cups.IPP_JOB_CANCELED) == cups.IPP_JOB_HELD:
  864.             if job.get ('job-hold-until', 'none') == 'auth-info-required':
  865.                 authenticate.set_sensitive (True)
  866.  
  867.     def show_treeview_popup_menu (self, treeview, event, event_button):
  868.         # Right-clicked.
  869.         self.job_context_menu.popup (None, None, None, event_button,
  870.                                      event.get_time ())
  871.  
  872.     def on_icon_popupmenu(self, icon, button, time):
  873.         self.statusicon_popupmenu.popup (None, None, None, button, time)
  874.  
  875.     def on_icon_hide_activate(self, menuitem):
  876.         self.num_jobs_when_hidden = len (self.jobs.keys ())
  877.         self.set_statusicon_visibility ()
  878.  
  879.     def on_icon_configure_printers_activate(self, menuitem):
  880.         if self.loop:
  881.             env = {}
  882.             for name, value in os.environ.iteritems ():
  883.                 if name == "SYSTEM_CONFIG_PRINTER_GLADE":
  884.                     continue
  885.                 env[name] = value
  886.             p = subprocess.Popen ([ "system-config-printer" ],
  887.                                   close_fds=True, env=env)
  888.             gobject.timeout_add (10 * 1000, self.poll_subprocess, p)
  889.  
  890.     def poll_subprocess(self, process):
  891.         returncode = process.poll ()
  892.         return returncode == None
  893.  
  894.     def on_icon_quit_activate (self, menuitem):
  895.         if self.loop:
  896.             self.loop.quit ()
  897.  
  898.     def on_job_cancel_activate(self, menuitem):
  899.         dialog = gtk.Dialog (_("Cancel Job"), self.JobsWindow,
  900.                              gtk.DIALOG_MODAL |
  901.                              gtk.DIALOG_DESTROY_WITH_PARENT |
  902.                              gtk.DIALOG_NO_SEPARATOR,
  903.                              (gtk.STOCK_NO, gtk.RESPONSE_NO,
  904.                               gtk.STOCK_YES, gtk.RESPONSE_YES))
  905.         dialog.set_default_response (gtk.RESPONSE_NO)
  906.         dialog.set_border_width (6)
  907.         dialog.set_resizable (False)
  908.         hbox = gtk.HBox (False, 12)
  909.         image = gtk.Image ()
  910.         image.set_from_stock (gtk.STOCK_DIALOG_QUESTION, gtk.ICON_SIZE_DIALOG)
  911.         image.set_alignment (0.0, 0.0)
  912.         hbox.pack_start (image, False, False, 0)
  913.         label = gtk.Label (_("Do you really want to cancel this job?"))
  914.         label.set_line_wrap (True)
  915.         label.set_alignment (0.0, 0.0)
  916.         hbox.pack_start (label, False, False, 0)
  917.         dialog.vbox.pack_start (hbox, False, False, 0)
  918.         dialog.set_data ('job-id', self.jobid)
  919.         dialog.connect ("response", self.on_job_cancel_prompt_response)
  920.         dialog.connect ("delete-event", self.on_job_cancel_prompt_delete)
  921.         dialog.show_all ()
  922.  
  923.     def on_job_cancel_prompt_delete (self, dialog, event):
  924.         self.on_job_cancel_prompt_response (dialog, gtk.RESPONSE_NO)
  925.  
  926.     def on_job_cancel_prompt_response (self, dialog, response):
  927.         jobid = dialog.get_data ('job-id')
  928.         dialog.destroy ()
  929.  
  930.         if response != gtk.RESPONSE_YES:
  931.             return
  932.  
  933.         try:
  934.             c = authconn.Connection (self.JobsWindow,
  935.                                      host=self.host,
  936.                                      port=self.port,
  937.                                      encryption=self.encryption)
  938.         except RuntimeError:
  939.             return
  940.  
  941.         c._begin_operation (_("canceling job"))
  942.         try:
  943.             c.cancelJob (jobid)
  944.         except cups.IPPError, (e, m):
  945.             if (e != cups.IPP_NOT_POSSIBLE and
  946.                 e != cups.IPP_NOT_FOUND):
  947.                 self.show_IPP_Error (e, m)
  948.             self.monitor.update ()
  949.             c._end_operation ()
  950.             return
  951.  
  952.         c._end_operation ()
  953.         del c
  954.         self.monitor.update ()
  955.  
  956.     def on_job_hold_activate(self, menuitem):
  957.         try:
  958.             c = authconn.Connection (self.JobsWindow,
  959.                                      host=self.host,
  960.                                      port=self.port,
  961.                                      encryption=self.encryption)
  962.         except RuntimeError:
  963.             return
  964.  
  965.         c._begin_operation (_("holding job"))
  966.         try:
  967.             c.setJobHoldUntil (self.jobid, "indefinite")
  968.         except cups.IPPError, (e, m):
  969.             if (e != cups.IPP_NOT_POSSIBLE and
  970.                 e != cups.IPP_NOT_FOUND):
  971.                 self.show_IPP_Error (e, m)
  972.             self.monitor.update ()
  973.             c._end_operation ()
  974.             return
  975.  
  976.         c._end_operation ()
  977.         del c
  978.         self.monitor.update ()
  979.  
  980.     def on_job_release_activate(self, menuitem):
  981.         try:
  982.             c = authconn.Connection (self.JobsWindow,
  983.                                      host=self.host,
  984.                                      port=self.port,
  985.                                      encryption=self.encryption)
  986.         except RuntimeError:
  987.             return
  988.  
  989.         c._begin_operation (_("releasing job"))
  990.         try:
  991.             c.setJobHoldUntil (self.jobid, "no-hold")
  992.         except cups.IPPError, (e, m):
  993.             if (e != cups.IPP_NOT_POSSIBLE and
  994.                 e != cups.IPP_NOT_FOUND):
  995.                 self.show_IPP_Error (e, m)
  996.             self.monitor.update ()
  997.             c._end_operation ()
  998.             return
  999.  
  1000.         c._end_operation ()
  1001.         del c
  1002.         self.monitor.update ()
  1003.  
  1004.     def on_job_reprint_activate(self, menuitem):
  1005.         try:
  1006.             c = authconn.Connection (self.JobsWindow,
  1007.                                      host=self.host,
  1008.                                      port=self.port,
  1009.                                      encryption=self.encryption)
  1010.             c.restartJob (self.jobid)
  1011.             del c
  1012.         except cups.IPPError, (e, m):
  1013.             self.show_IPP_Error (e, m)
  1014.             self.monitor.update ()
  1015.             return
  1016.         except RuntimeError:
  1017.             return
  1018.  
  1019.         self.monitor.update ()
  1020.  
  1021.     def on_job_authenticate_activate(self, menuitem):
  1022.         self.display_auth_info_dialog (self.jobid)
  1023.  
  1024.     def on_refresh_activate(self, menuitem):
  1025.         self.monitor.refresh ()
  1026.  
  1027.     def job_is_active (self, jobdata):
  1028.         state = jobdata.get ('job-state', cups.IPP_JOB_CANCELED)
  1029.         if state >= cups.IPP_JOB_CANCELED:
  1030.             return False
  1031.  
  1032.         return True
  1033.  
  1034.     ## Icon manipulation
  1035.     def add_state_reason_emblem (self, pixbuf):
  1036.         if self.worst_reason != None:
  1037.             # Check that it's valid.
  1038.             printer = self.worst_reason.get_printer ()
  1039.             found = False
  1040.             for reason in self.printer_state_reasons[printer]:
  1041.                 if reason == self.worst_reason:
  1042.                     found = True
  1043.                     break
  1044.             if not found:
  1045.                 self.worst_reason = None
  1046.  
  1047.         if self.worst_reason != None:
  1048.             level = self.worst_reason.get_level ()
  1049.             if level > StateReason.REPORT:
  1050.                 # Add an emblem to the icon.
  1051.                 icon = StateReason.LEVEL_ICON[level]
  1052.                 pixbuf = pixbuf.copy ()
  1053.                 theme = gtk.icon_theme_get_default ()
  1054.  
  1055.                 emblem = theme.load_icon (icon, 22, 0)
  1056.                 emblem.composite (pixbuf,
  1057.                                   pixbuf.get_width () / 2,
  1058.                                   pixbuf.get_height () / 2,
  1059.                                   emblem.get_width () / 2,
  1060.                                   emblem.get_height () / 2,
  1061.                                   pixbuf.get_width () / 2,
  1062.                                   pixbuf.get_height () / 2,
  1063.                                   0.5, 0.5,
  1064.                                   gtk.gdk.INTERP_BILINEAR, 255)
  1065.  
  1066.         return pixbuf
  1067.  
  1068.     def get_icon_pixbuf (self, have_jobs=None):
  1069.         if not self.trayicon:
  1070.             return
  1071.  
  1072.         if have_jobs == None:
  1073.             have_jobs = len (self.jobs.keys ()) > 0
  1074.  
  1075.         if have_jobs:
  1076.             pixbuf = self.icon_jobs
  1077.             for jobid, jobdata in self.jobs.iteritems ():
  1078.                 jstate = jobdata.get ('job-state', cups.IPP_JOB_PENDING)
  1079.                 if jstate == cups.IPP_JOB_PROCESSING:
  1080.                     pixbuf = self.icon_jobs_processing
  1081.                     break
  1082.         else:
  1083.             pixbuf = self.icon_no_jobs
  1084.  
  1085.         try:
  1086.             pixbuf = self.add_state_reason_emblem (pixbuf)
  1087.         except:
  1088.             nonfatalException ()
  1089.  
  1090.         return pixbuf
  1091.  
  1092.     def set_statusicon_tooltip (self, tooltip=None):
  1093.         if not self.trayicon:
  1094.             return
  1095.  
  1096.         if tooltip == None:
  1097.             num_jobs = len (self.jobs)
  1098.             if num_jobs == 0:
  1099.                 tooltip = _("No documents queued")
  1100.             elif num_jobs == 1:
  1101.                 tooltip = _("1 document queued")
  1102.             else:
  1103.                 tooltip = _("%d documents queued") % num_jobs
  1104.  
  1105.         self.statusicon.set_tooltip (tooltip)
  1106.  
  1107.     def update_status (self, have_jobs=None):
  1108.         # Found out which printer state reasons apply to our active jobs.
  1109.         upset_printers = set()
  1110.         for printer, reasons in self.printer_state_reasons.iteritems ():
  1111.             if len (reasons) > 0:
  1112.                 upset_printers.add (printer)
  1113.         debugprint ("Upset printers: %s" % upset_printers)
  1114.  
  1115.         my_upset_printers = set()
  1116.         if len (upset_printers):
  1117.             my_upset_printers = set()
  1118.             for jobid in self.active_jobs:
  1119.                 # 'job-printer-name' is set by job_added/job_event
  1120.                 printer = self.jobs[jobid]['job-printer-name']
  1121.                 if printer in upset_printers:
  1122.                     my_upset_printers.add (printer)
  1123.             debugprint ("My upset printers: %s" % my_upset_printers)
  1124.  
  1125.         my_reasons = []
  1126.         for printer in my_upset_printers:
  1127.             my_reasons.extend (self.printer_state_reasons[printer])
  1128.  
  1129.         # Find out which is the most problematic.
  1130.         self.worst_reason = None
  1131.         if len (my_reasons) > 0:
  1132.             worst_reason = my_reasons[0]
  1133.             for reason in my_reasons:
  1134.                 if reason > worst_reason:
  1135.                     worst_reason = reason
  1136.             self.worst_reason = worst_reason
  1137.             debugprint ("Worst reason: %s" % worst_reason)
  1138.  
  1139.         if self.worst_reason != None:
  1140.             (title, tooltip) = self.worst_reason.get_description ()
  1141.             if self.statusbar_set:
  1142.                 self.statusbar.pop (0)
  1143.             self.statusbar.push (0, tooltip)
  1144.             self.statusbar_set = True
  1145.         else:
  1146.             tooltip = None
  1147.             if self.statusbar_set:
  1148.                 self.statusbar.pop (0)
  1149.                 self.statusbar_set = False
  1150.  
  1151.         if self.trayicon:
  1152.             pixbuf = self.get_icon_pixbuf (have_jobs=have_jobs)
  1153.             self.set_statusicon_from_pixbuf (pixbuf)
  1154.             self.set_statusicon_visibility ()
  1155.             self.set_statusicon_tooltip (tooltip=tooltip)
  1156.  
  1157.     ## Notifications
  1158.     def notify_printer_state_reason_if_important (self, reason):
  1159.         level = reason.get_level ()
  1160.         if level < StateReason.WARNING:
  1161.             # Not important enough to justify a notification.
  1162.             return
  1163.  
  1164.         self.notify_printer_state_reason (reason)
  1165.  
  1166.     def notify_printer_state_reason (self, reason):
  1167.         tuple = reason.get_tuple ()
  1168.         if self.state_reason_notifications.has_key (tuple):
  1169.             debugprint ("Already sent notification for %s" % repr (reason))
  1170.             return
  1171.  
  1172.         level = reason.get_level ()
  1173.         if (level == StateReason.ERROR or
  1174.             reason.get_reason () == "connecting-to-device"):
  1175.             urgency = pynotify.URGENCY_NORMAL
  1176.         else:
  1177.             urgency = pynotify.URGENCY_LOW
  1178.  
  1179.         (title, text) = reason.get_description ()
  1180.         notification = pynotify.Notification (title, text, 'printer')
  1181.         reason.user_notified = True
  1182.         notification.set_urgency (urgency)
  1183.         if "actions" in pynotify.get_server_caps():
  1184.             notification.set_timeout (pynotify.EXPIRES_NEVER)
  1185.         notification.connect ('closed',
  1186.                               self.on_state_reason_notification_closed)
  1187.         self.state_reason_notifications[reason.get_tuple ()] = notification
  1188.         self.set_statusicon_visibility ()
  1189.         notification.attach_to_status_icon (self.statusicon)
  1190.         notification.show ()
  1191.  
  1192.     def on_state_reason_notification_closed (self, notification, reason=None):
  1193.         debugprint ("Notification %s closed" % repr (notification))
  1194.         notification.set_data ('closed', True)
  1195.         self.set_statusicon_visibility ()
  1196.         return
  1197.  
  1198.     def notify_completed_job (self, jobid):
  1199.         job = self.jobs.get (jobid, {})
  1200.         document = job.get ('job-name', _("Unknown"))
  1201.         printer = job.get ('job-printer-name', _("Unknown"))
  1202.         notification = pynotify.Notification (_("Job %d completed") % jobid,
  1203.                                               _("Document `%s' has finished "
  1204.                                                 "printing on `%s'.") %
  1205.                                               (document, printer),
  1206.                                               'printer')
  1207.         notification.set_urgency (pynotify.URGENCY_LOW)
  1208.         notification.connect ('closed',
  1209.                               self.on_completed_job_notification_closed)
  1210.         notification.set_data ('jobid', jobid)
  1211.         self.completed_job_notifications[jobid] = notification
  1212.         self.set_statusicon_visibility ()
  1213.         notification.attach_to_status_icon (self.statusicon)
  1214.         notification.show ()
  1215.  
  1216.     def on_completed_job_notification_closed (self, notification, reason=None):
  1217.         jobid = notification.get_data ('jobid')
  1218.         del self.completed_job_notifications[jobid]
  1219.         self.set_statusicon_visibility ()
  1220.  
  1221.     ## monitor.Watcher interface
  1222.     def current_printers_and_jobs (self, mon, printers, jobs):
  1223.         monitor.Watcher.current_printers_and_jobs (self, mon, printers, jobs)
  1224.         self.set_process_pending (False)
  1225.         self.store.clear ()
  1226.         self.jobs = {}
  1227.         self.jobiters = {}
  1228.         self.printer_uri_index = PrinterURIIndex (names=printers)
  1229.         connection = None
  1230.         for jobid, jobdata in jobs.iteritems ():
  1231.             uri = jobdata.get ('job-printer-uri', '')
  1232.             try:
  1233.                 printer = self.printer_uri_index.lookup (uri,
  1234.                                                          connection=connection)
  1235.             except KeyError:
  1236.                 printer = uri
  1237.             jobdata['job-printer-name'] = printer
  1238.  
  1239.             self.add_job (jobid, jobdata, connection=connection)
  1240.  
  1241.         self.jobs = jobs
  1242.         self.active_jobs = set()
  1243.         for jobid, jobdata in jobs.iteritems ():
  1244.             if self.job_is_active (jobdata):
  1245.                 self.active_jobs.add (jobid)
  1246.  
  1247.         self.set_process_pending (True)
  1248.         self.update_status ()
  1249.  
  1250.     def job_added (self, mon, jobid, eventname, event, jobdata):
  1251.         monitor.Watcher.job_added (self, mon, jobid, eventname, event, jobdata)
  1252.  
  1253.         uri = jobdata.get ('job-printer-uri', '')
  1254.         try:
  1255.             printer = self.printer_uri_index.lookup (uri)
  1256.         except KeyError:
  1257.             printer = uri
  1258.         jobdata['job-printer-name'] = printer
  1259.  
  1260.         # We may be showing this job already, perhaps because we are showing
  1261.         # completed jobs and one was reprinted.
  1262.         if not self.jobiters.has_key (jobid):
  1263.             self.add_job (jobid, jobdata)
  1264.  
  1265.         if self.job_is_active (jobdata):
  1266.             self.active_jobs.add (jobid)
  1267.         elif jobid in self.active_jobs:
  1268.             self.active_jobs.remove (jobid)
  1269.  
  1270.         self.update_status (have_jobs=True)
  1271.         if self.trayicon:
  1272.             if not self.job_is_active (jobdata):
  1273.                 return
  1274.  
  1275.             for reason in self.printer_state_reasons.get (printer, []):
  1276.                 if not reason.user_notified:
  1277.                     self.notify_printer_state_reason_if_important (reason)
  1278.  
  1279.     def job_event (self, mon, jobid, eventname, event, jobdata):
  1280.         monitor.Watcher.job_event (self, mon, jobid, eventname, event, jobdata)
  1281.  
  1282.         uri = jobdata.get ('job-printer-uri', '')
  1283.         try:
  1284.             printer = self.printer_uri_index.lookup (uri)
  1285.         except KeyError:
  1286.             printer = uri
  1287.         jobdata['job-printer-name'] = printer
  1288.  
  1289.         any_active = len (self.active_jobs) > 0
  1290.         if self.job_is_active (jobdata):
  1291.             self.active_jobs.add (jobid)
  1292.         elif jobid in self.active_jobs:
  1293.             self.active_jobs.remove (jobid)
  1294.         if (len (self.active_jobs) > 0) != any_active:
  1295.             self.update_status ()
  1296.  
  1297.         self.update_job (jobid, jobdata)
  1298.         jobdata = self.jobs[jobid]
  1299.  
  1300.         # If the job has finished, let the user know.
  1301.         if self.trayicon and (eventname == 'job-completed' or
  1302.                               (eventname == 'job-state-changed' and
  1303.                                event['job-state'] == cups.IPP_JOB_COMPLETED)):
  1304.             reasons = event['job-state-reasons']
  1305.             if type (reasons) != list:
  1306.                 reasons = [reasons]
  1307.  
  1308.             canceled = False
  1309.             for reason in reasons:
  1310.                 if reason.startswith ("job-canceled"):
  1311.                     canceled = True
  1312.                     break
  1313.  
  1314.             if not canceled:
  1315.                 self.notify_completed_job (jobid)
  1316.  
  1317.         # Look out for stopped jobs.
  1318.         if (self.trayicon and eventname == 'job-stopped' and
  1319.             not jobid in self.stopped_job_prompts):
  1320.             # Why has the job stopped?  It might be due to a job error
  1321.             # of some sort, or it might be that the backend requires
  1322.             # authentication.  If the latter, the job will be held not
  1323.             # stopped, and the job-hold-until attribute will be
  1324.             # 'auth-info-required'.  This was already checked for in
  1325.             # update_job.
  1326.             may_be_problem = True
  1327.             jstate = jobdata['job-state']
  1328.             if (jstate in [cups.IPP_JOB_PENDING, cups.IPP_JOB_PROCESSING] or
  1329.                 (jstate == cups.IPP_JOB_HELD and
  1330.                  jobdata['job-hold-until'] == 'auth-info-required')):
  1331.                 # update_job already dealt with this.
  1332.                 may_be_problem = False
  1333.             else:
  1334.                 # Other than that, unfortunately the only
  1335.                 # clue we get is the notify-text, which is not
  1336.                 # translated into our native language.  We'd better
  1337.                 # try parsing it.  In CUPS-1.3.6 the possible strings
  1338.                 # are:
  1339.                 #
  1340.                 # "Job stopped due to filter errors; please consult
  1341.                 # the error_log file for details."
  1342.                 #
  1343.                 # "Job stopped due to backend errors; please consult
  1344.                 # the error_log file for details."
  1345.                 #
  1346.                 # "Job held due to backend errors; please consult the
  1347.                 # error_log file for details."
  1348.                 #
  1349.                 # "Authentication is required for job %d."
  1350.                 # [This case is handled in the update_job method.]
  1351.                 #
  1352.                 # "Job stopped due to printer being paused"
  1353.                 # [This should be ignored, as the job was doing just
  1354.                 # fine until the printer was stopped for other reasons.]
  1355.                 notify_text = event['notify-text']
  1356.                 document = jobdata['job-name']
  1357.                 if notify_text.find ("backend errors") != -1:
  1358.                     message = _("There was a problem sending document `%s' "
  1359.                                 "(job %d) to the printer.") % (document, jobid)
  1360.                 elif notify_text.find ("filter errors") != -1:
  1361.                     message = _("There was a problem processing document `%s' "
  1362.                                 "(job %d).") % (document, jobid)
  1363.                 elif notify_text.find ("being paused") != -1:
  1364.                     may_be_problem = False
  1365.                 else:
  1366.                     # Give up and use the provided message untranslated.
  1367.                     message = _("There was a problem printing document `%s' "
  1368.                                 "(job %d): `%s'.") % (document, jobid,
  1369.                                                       notify_text)
  1370.  
  1371.             if may_be_problem:
  1372.                 debugprint ("Problem detected")
  1373.                 self.toggle_window_display (self.statusicon, force_show=True)
  1374.                 dialog = gtk.Dialog (_("Print Error"), self.JobsWindow, 0,
  1375.                                      (_("_Diagnose"), gtk.RESPONSE_NO,
  1376.                                         gtk.STOCK_OK, gtk.RESPONSE_OK))
  1377.                 dialog.set_default_response (gtk.RESPONSE_OK)
  1378.                 dialog.set_border_width (6)
  1379.                 dialog.set_resizable (False)
  1380.                 dialog.set_icon_name (ICON)
  1381.                 hbox = gtk.HBox (False, 12)
  1382.                 hbox.set_border_width (6)
  1383.                 image = gtk.Image ()
  1384.                 image.set_from_stock (gtk.STOCK_DIALOG_ERROR,
  1385.                                       gtk.ICON_SIZE_DIALOG)
  1386.                 hbox.pack_start (image, False, False, 0)
  1387.                 vbox = gtk.VBox (False, 12)
  1388.  
  1389.                 markup = ('<span weight="bold" size="larger">' +
  1390.                           _("Print Error") + '</span>\n\n' +
  1391.                           message)
  1392.                 try:
  1393.                     if event['printer-state'] == cups.IPP_PRINTER_STOPPED:
  1394.                         name = event['printer-name']
  1395.                         markup += ' '
  1396.                         markup += (_("The printer called `%s' has "
  1397.                                      "been disabled.") % name)
  1398.                 except KeyError:
  1399.                     pass
  1400.  
  1401.                 label = gtk.Label (markup)
  1402.                 label.set_use_markup (True)
  1403.                 label.set_line_wrap (True)
  1404.                 label.set_alignment (0, 0)
  1405.                 vbox.pack_start (label, False, False, 0)
  1406.                 hbox.pack_start (vbox, False, False, 0)
  1407.                 dialog.vbox.pack_start (hbox)
  1408.                 dialog.connect ('response',
  1409.                                 self.print_error_dialog_response, jobid)
  1410.                 self.stopped_job_prompts.add (jobid)
  1411.                 dialog.show_all ()
  1412.  
  1413.     def job_removed (self, mon, jobid, eventname, event):
  1414.         monitor.Watcher.job_removed (self, mon, jobid, eventname, event)
  1415.  
  1416.         # If the job has finished, let the user know.
  1417.         if self.trayicon and (eventname == 'job-completed' or
  1418.                               (eventname == 'job-state-changed' and
  1419.                                event['job-state'] == cups.IPP_JOB_COMPLETED)):
  1420.             reasons = event['job-state-reasons']
  1421.             debugprint (reasons)
  1422.             if type (reasons) != list:
  1423.                 reasons = [reasons]
  1424.  
  1425.             canceled = False
  1426.             for reason in reasons:
  1427.                 if reason.startswith ("job-canceled"):
  1428.                     canceled = True
  1429.                     break
  1430.  
  1431.             if not canceled:
  1432.                 self.notify_completed_job (jobid)
  1433.  
  1434.         if self.jobiters.has_key (jobid):
  1435.             self.store.remove (self.jobiters[jobid])
  1436.             del self.jobiters[jobid]
  1437.             del self.jobs[jobid]
  1438.  
  1439.         if jobid in self.active_jobs:
  1440.             self.active_jobs.remove (jobid)
  1441.  
  1442.         self.update_status ()
  1443.  
  1444.     def state_reason_added (self, mon, reason):
  1445.         monitor.Watcher.state_reason_added (self, mon, reason)
  1446.  
  1447.         (title, text) = reason.get_description ()
  1448.         printer = reason.get_printer ()
  1449.  
  1450.         try:
  1451.             l = self.printer_state_reasons[printer]
  1452.         except KeyError:
  1453.             l = []
  1454.             self.printer_state_reasons[printer] = l
  1455.  
  1456.         reason.user_notified = False
  1457.         l.append (reason)
  1458.         self.update_status ()
  1459.  
  1460.         if not self.trayicon:
  1461.             return
  1462.  
  1463.         # Find out if the user has jobs queued for that printer.
  1464.         for job, data in self.jobs.iteritems ():
  1465.             if not self.job_is_active (data):
  1466.                 continue
  1467.             if data['job-printer-name'] == printer:
  1468.                 # Yes!  Notify them of the state reason, if necessary.
  1469.                 self.notify_printer_state_reason_if_important (reason)
  1470.                 break
  1471.  
  1472.     def state_reason_removed (self, mon, reason):
  1473.         monitor.Watcher.state_reason_removed (self, mon, reason)
  1474.  
  1475.         printer = reason.get_printer ()
  1476.         try:
  1477.             reasons = self.printer_state_reasons[printer]
  1478.         except KeyError:
  1479.             debugprint ("Printer not found")
  1480.             return
  1481.  
  1482.         try:
  1483.             i = reasons.index (reason)
  1484.         except IndexError:
  1485.             debugprint ("Reason not found")
  1486.             return
  1487.  
  1488.         del reasons[i]
  1489.  
  1490.         self.update_status ()
  1491.  
  1492.         if not self.trayicon:
  1493.             return
  1494.  
  1495.         tuple = reason.get_tuple ()
  1496.         try:
  1497.             notification = self.state_reason_notifications[tuple]
  1498.             if notification.get_data ('closed') != True:
  1499.                 notification.close ()
  1500.             del self.state_reason_notifications[tuple]
  1501.             self.set_statusicon_visibility ()
  1502.         except KeyError:
  1503.             pass
  1504.  
  1505.     def still_connecting (self, mon, reason):
  1506.         monitor.Watcher.still_connecting (self, mon, reason)
  1507.         if not self.trayicon:
  1508.             return
  1509.  
  1510.         self.notify_printer_state_reason (reason)
  1511.  
  1512.     def now_connected (self, mon, printer):
  1513.         monitor.Watcher.now_connected (self, mon, printer)
  1514.  
  1515.         if not self.trayicon:
  1516.             return
  1517.  
  1518.         # Find the connecting-to-device state reason.
  1519.         try:
  1520.             reasons = self.printer_state_reasons[printer]
  1521.             reason = None
  1522.             for r in reasons:
  1523.                 if r.get_reason () == "connecting-to-device":
  1524.                     reason = r
  1525.                     break
  1526.         except KeyError:
  1527.             debugprint ("Couldn't find state reason (no reasons)!")
  1528.  
  1529.         if reason != None:
  1530.             tuple = reason.get_tuple ()
  1531.         else:
  1532.             debugprint ("Couldn't find state reason in list!")
  1533.             for (level,
  1534.                  p,
  1535.                  r) in self.state_reason_notifications.keys ():
  1536.                 if p == printer and r == "connecting-to-device":
  1537.                     debugprint ("Found from notifications list")
  1538.                     tuple = (level, p, r)
  1539.                     break
  1540.  
  1541.         try:
  1542.             notification = self.state_reason_notifications[tuple]
  1543.         except KeyError:
  1544.             debugprint ("Unexpected now_connected signal")
  1545.             return
  1546.  
  1547.         if notification.get_data ('closed') != True:
  1548.             notification.close ()
  1549.             notification.set_data ('closed', True)
  1550.  
  1551.     def printer_event (self, mon, printer, eventname, event):
  1552.         monitor.Watcher.printer_event (self, mon, printer, eventname, event)
  1553.         self.printer_uri_index.update_from_attrs (printer, event)
  1554.  
  1555.     def printer_removed (self, mon, printer):
  1556.         monitor.Watcher.printer_removed (self, mon, printer)
  1557.         self.printer_uri_index.remove_printer (printer)
  1558.  
  1559.     ## Printer status window
  1560.     def set_printer_status_icon (self, column, cell, model, iter, *user_data):
  1561.         level = model.get_value (iter, 0)
  1562.         icon = StateReason.LEVEL_ICON[level]
  1563.         theme = gtk.icon_theme_get_default ()
  1564.         try:
  1565.             pixbuf = theme.load_icon (icon, 22, 0)
  1566.             cell.set_property("pixbuf", pixbuf)
  1567.         except gobject.GError, exc:
  1568.             pass # Couldn't load icon
  1569.  
  1570.     def set_printer_status_name (self, column, cell, model, iter, *user_data):
  1571.         cell.set_property("text", model.get_value (iter, 1))
  1572.